package view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.FileParser;
import model.Manuscript;
import model.State;
import model.Status;
import model.User;

/**
 * Class to create an add reviewer frame.
 * @author Kevin Anderson
 * @version 12/5/2012
 */
@SuppressWarnings("serial")
public class AddReviewerFrame extends JFrame {

	/**
	 * Field to hold the manuscript that will be assigned to a reviewer.
	 */
	private Manuscript my_manuscript;
	
	/**
	 * Field to hold the current state of the system.
	 */
	private State my_state;

	/**
	 * Constructor for the add reviewer JFrame.
	 * @param manuscript the manuscript to be assigned.
	 * @param the_state the current state of the system.
	 */
	public AddReviewerFrame(Manuscript manuscript, State the_state) {

		super("Add Reviewer");

		my_manuscript = manuscript;
		my_state = the_state;
		setResizable(true); 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
		setLocationRelativeTo(null);
		setVisible(true);
		setup();
	}

	/**
	 * Method to setup the JFrame.
	 */
	public void setup() {
		add(new AddReviewerPanel());
		pack();

	}


	/**
	 * Class to create a add reviewer panel
	 * @author Kevin Anderson
	 * @version 12/5/2012
	 */
	private class AddReviewerPanel extends JPanel
	{
		/**
		 * Field to hold the combobox of possible users.
		 */
		private JComboBox<User> my_user_comboBox;

		/**
		 * Constructor for an add reviewer panel.
		 */
		public AddReviewerPanel() {
			super();
			setup();
		}

		/**
		 * Method to setup the JPanel.
		 */
		public void setup() {
			GridLayout exp_layout = new GridLayout(0,2);
			this.setLayout(exp_layout);

			this.add(new JLabel("Choose a Reviewer: "));
			setupBox();

			this.add(new JButton(new CloseAction(AddReviewerFrame.this)));

			//ONLY ALLOW ASSIGN IF THERE ARE OPTIONS
			if(my_user_comboBox.getItemCount() > 0)
				this.add(new JButton(new AssignAction()));
		}

		/**
		 * Method to setup the combobox of users.
		 */
		public void setupBox() {

			my_user_comboBox = new JComboBox<User>();

			//GET A LIST OF POSSIBLE REVIEWERS
			User[] possibleUsers = my_state.getParser().getReviewersByConf(my_state.getConference().getConfID());


			for (int i = 0; i < possibleUsers.length; i++) {
				//DO NOT ALLOW REVIEWERS TO BE ASSIGNED THEIR OWN PAPER.

				int revCount = my_state.getParser().getReviewCountForReviewer(
						my_state.getConference().getConfID(), possibleUsers[i].getID());

				if (my_manuscript.getMy_author() != possibleUsers[i].getID() &&
						revCount < 4)
				{

					my_user_comboBox.addItem(possibleUsers[i]);
				}
			}

			//NOTIFY THE USER IF THE DROPDOWN IS BLANK.
			if(my_user_comboBox.getItemCount() == 0)
			{
				JOptionPane.showMessageDialog(this, "There are no valid SPCs to assign");
				AddReviewerFrame.this.dispose();
			}

			my_user_comboBox.setVisible(true);
			add(my_user_comboBox);
			this.validate();
		}

		/**
		 * Class to assign a reviewer to a manuscript.
		 * @author Kevin Anderson
		 * @version 12/5/2012
		 */
		private class AssignAction extends AbstractAction
		{

			/**
			 * Constructor for an assign action.
			 */
			public AssignAction() {
				super("Assign");
				putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A);
				putValue(Action.SELECTED_KEY, true);
			}

			/**
			 * Method to assign a reviewer to a manuscript.
			 * @param arg0 the action event that triggers the method.
			 */
			@Override
			public void actionPerformed(ActionEvent arg0) {
				FileParser fp = my_state.getParser();
				if(fp.addReviewer(my_manuscript, ((User) my_user_comboBox.getSelectedItem()).getID())) //CALL ADD REVIEWER TO PAPER
				{
					JOptionPane.showMessageDialog(AddReviewerFrame.this, "You Successfully assigned the Reviewer.");
					//my_manuscript.setMy_status(Status.ASSIGNED);
					fp.writeManuscript(my_manuscript);
				} else {
					JOptionPane.showMessageDialog(AddReviewerFrame.this, "The Reviewer was not assigned.");
				}

				AddReviewerFrame.this.dispose();
			}
		}//End Class assign action.
	}//End class AddReviewerPanel.
}//End class Add reviewer frame.